//
//  LogicCommendDataSource.m
//  tcpoctest
//
//  Created by gump on 5/21/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "LogicCommendDataSource.h"

#import "sendconfig.h"
#import "sendLoginBypp.h"
#import "sendLogin.h"
#import "sendPotocol.h"
#import "sendFirCirGetBgImage.h"
#import "sendFirCirBgImage.h"
#import "sendnearpeople.h"
#import "sendAddFirend.h"
#import "sendFirendSearch.h"
#import "sendFirendDelete.h"
#import "sendPersonSetName.h"
#import "sendPersonPassWord.h"
#import "sendPersonSetSex.h"
#import "sendPersonSetAge.h"
#import "sendPersonSetPC.h"
#import "sendPersonSetCheck.h"
#import "sendPersonSetSign.h"
#import "sendPersonHeadImage.h"
#import "sendPersonSetMail.h"
#import "sendChatText.h"
#import "sendChatImage.h"
#import "sendChatVoice.h"
#import "sendHeadImageSmall.h"
#import "sendSearchPPNum.h"
#import "sendReport.h"
#import "sendChatRead.h"

#import "userdb.h"
#import "bigimagedb.h"

#import "PromptLoginDataSource.h"
#import "headimagedb.h"
#import "FirCirLocalResourceDataSource.h"
#import "FirCirBgImageDataSource.h"
#import "FirendCircleDataSource.h"
#import "GiftListDataSource.h"
#import "GiftInfoDataSource.h"
#import "FirCirPersonDataSource.h"

#import "BigImageInfo.h"

#import "chatDataSource.h"
#import "ChatContentInfo.h"
#import "chatuserdb.h"

#import "firendCircledb.h"
#import "FirendCirLocalPathdb.h"
#import "FirCirCommentdb.h"
#import "FirCirPictureIDdb.h"
#import "fircirnewsdb.h"
#import "FirCirNewIDCommentIDdb.h"
#import "FirCirMailNewsdb.h"
#import "giftInfoImageDataBase.h"

#import "FirCirNewsDataSource.h"
#import "FirCirNewsMailDataSource.h"

#import "makeuid.h"

#import "giftInfoMusicDataBase.h"
#import "bigHeadImageDataSource.h"

@implementation LogicCommendDataSource

-(id)init
{
    self = [super init];
    if (self != nil) {
        LogicCommandArr = [[NSMutableArray alloc] init];
        pthread_mutex_init(&mutexLC, 0);
    }
    
    return self;
}

-(void)addLogicCommand:(int)command
{
    pthread_mutex_lock(&mutexLC);
    
    [LogicCommandArr addObject:[NSNumber numberWithInt:command]];
    
    pthread_mutex_unlock(&mutexLC);
}

-(void)processLC
{
    pthread_mutex_lock(&mutexLC);
    
    if ([LogicCommandArr count] > 0)
    {
        NSNumber *icommand = [LogicCommandArr objectAtIndex:0];
        [self processCommand:[icommand intValue]];
        
        [LogicCommandArr removeObjectAtIndex:0];
    }
    
    pthread_mutex_unlock(&mutexLC);
}

-(void)processCommand:(int)icommand
{
    switch (icommand) {
        case LC_HEADJUMP:
        {
            [[netManager sharedNetManager] SendHeartBeat];
        }
            break;
        case LC_SENDCONFIG:
        {
            NSString * uid = [[makeuid sharedmake] getui];
            UID_LOG(@"config uid %@",uid);
            [[netManager sharedNetManager].sendc SendConfigP:[DataControl shareControl].personDS.uinfo.mail
                                                      withPS:[DataControl shareControl].personDS.uinfo.ps
                                                    withName:[DataControl shareControl].personDS.uinfo.name
                                                     withSex:[DataControl shareControl].personDS.uinfo.sex
                                                     withAge:[DataControl shareControl].personDS.uinfo.age
                                                withProvince:[DataControl shareControl].personDS.uinfo.province
                                                    withCity:[DataControl shareControl].personDS.uinfo.city
                                                     withUID:uid];
        }
            break;
        case LC_SEARCHFRIEND:
        {
            [[netManager sharedNetManager].sendSPP SendSearchPPNumP:[DataControl shareControl].iSerchPPNum];
        }
            break;
        case LC_ADDFIREND:
        {
            [[netManager sharedNetManager].sendFirendA SendAddfirendP:[DataControl shareControl].currentFI.mail];
        }
            break;
        case LC_DELETEFIREND:
        {
            [[netManager sharedNetManager].sendFirendD SendDeleteFirend:[DataControl shareControl].currentFI.mail];
        }
            break;
        case LC_CAR:
        {
            [[netManager sharedNetManager].sendFirendS
             SendSearchFirendP:[DataControl shareControl].personDS.strcurRegisterMail];
        }
            break;
        case LC_SENDPERSONINFO:
        {
            [[netManager sharedNetManager].sendp SendMainIndex:ID_MI_PERSONINFO
                                               WithSecondIndex:PI_REQUEST];
            
            
            NSString *url = [[DataControl shareControl].HeadImageDic
                             objectForKey:[DataControl shareControl].personDS.uinfo.mail];
            if (url == nil)
            {
                if (![[DataControl shareControl].headImageRequestSet
                      containsObject:[DataControl shareControl].personDS.uinfo.mail]) {
                    
                    [[DataControl shareControl].headImageSendArr
                     addObject:[DataControl shareControl].personDS.uinfo.mail];
                    [[DataControl shareControl].headImageRequestSet
                     addObject:[DataControl shareControl].personDS.uinfo.mail];
                    for (int imageCnt = 0; imageCnt< [[DataControl shareControl].headImageSendArr count]; imageCnt++) {
                        NSString *mail = [[DataControl shareControl].headImageSendArr objectAtIndex:imageCnt];
                        [[netManager sharedNetManager].sendHISmall SendRequestHeadImageP:mail];
                    }
                    
                    [[DataControl shareControl].headImageSendArr removeAllObjects];
                }
            }
        }
            break;
        case LC_SETNAME:
        {
            [[netManager sharedNetManager].sendPersonSN SendSetNameP:[DataControl shareControl].strChangeName];
        }
            break;
        case LC_UPDATEUIDB:
        {
            [[DataBaseCtrol shareControl].userdatabase updateUserInfo];
        }
            break;
        case LC_SETSEX:
        {
            [[netManager sharedNetManager].sendPersonSS SendSetSexP:[DataControl shareControl].ichangeSex];
        }
            break;
        case LC_SETPC:
        {
            [[netManager sharedNetManager].sendPersonSPC SendSetPCP:[DataControl shareControl].personDS.uinfo.province
                                             withCity:[DataControl shareControl].personDS.uinfo.city];
        }
            break;
        case LC_SETAGE:
        {
            [[netManager sharedNetManager].sendPersonA SendSetAgeP:[DataControl shareControl].ichangeAge];
        }
            break;
        case LC_SETPS:
        {
            [[netManager sharedNetManager].sendPersonPS SendSetPSP:
             [DataControl shareControl].strChangeOldPS
                                            withNewPS:
             [DataControl shareControl].strChangeNewPS];
        }
            break;
        case LC_SETHEADIMAGE:
        {
            [[netManager sharedNetManager].sendPersonHI SendHeadImageP];
        }
            break;
        case LC_SETSIGN:
        {
            [[netManager sharedNetManager].sendPersonSSign SendSetSign:[DataControl shareControl].strChangeSign];
        }
            break;
        case LC_BACKGROUND:
        {
            [[DataControl shareControl] SetGameState:GMS_CHANGEGROUND];
            
            [[netManager sharedNetManager] SendQuitDirectly];
            
            [[netManager sharedNetManager] closeConnect];
            
            
        }
            break;
        case LC_FOREGROUND:
        {
            [[netManager sharedNetManager] creatConnect];
            
            if ([DataControl shareControl].miOldGameState != GMS_LOGIN &&
                [DataControl shareControl].miOldGameState != GMS_CONFIG)
            {
                [[DataControl shareControl].promptloginDS SetIsShowMain:false];
                [[DataControl shareControl].promptloginDS SetIsPromptLogin:true];
                [[DataControl shareControl].promptloginDS SetIsInitData:false];
                
                [[DataControl shareControl].UICommendDS addUICommand:UC_LOCALLOGIN];
            }
            else
            {
                [[DataControl shareControl] SetGameState:[DataControl shareControl].miOldGameState];
            }
            
        }
            break;
        case LC_SETCHECK:
        {
            [[netManager sharedNetManager].sendPersonSC SendSetCheckP:[DataControl shareControl].ichangeNeedCheck];
        }
            break;
        case LC_LOGINBYPP:
        {
            [[DataControl shareControl] SetLoginType:LT_PP];
            
            [[netManager sharedNetManager].sendloginpp SendLoginWithPPP:
             [DataControl shareControl].personDS.uinfo.ppNumber
                                                                 withPS:
             [DataControl shareControl].personDS.uinfo.ps];
        }
            break;
        case LC_LOGINBYUI:
        {
            [[DataControl shareControl] SetLoginType:LT_MAIL];
            
            [[netManager sharedNetManager].sendlonginbymail sendLoginByMail:
             [DataControl shareControl].personDS.uinfo.mail
                                                                     withPS:
             [DataControl shareControl].personDS.uinfo.ps];
        }
            break;
        case LC_SENDTEXT:
        {
            if ([DataControl shareControl].currentFI != nil) {
                [[netManager sharedNetManager].sendCT SendChatText:[[DataControl shareControl].chatDS SendChatContent]
                                                   withMail:[DataControl shareControl].currentFI.mail];
            }
        }
            break;
        case LC_SENDIMAGE:
        {
            if ([DataControl shareControl].currentFI != nil) {
                [[netManager sharedNetManager].sendCI SendChatImage: [UIImage imageWithContentsOfFile:
                                                               [[DataControl shareControl].chatDS SendChatContent]]
                                                    withMail:[DataControl shareControl].currentFI.mail];
            }
            
        }
            break;
        case LC_SENDVOICE:
        {
            if ([DataControl shareControl].currentFI != nil) {
                [[netManager sharedNetManager].sendCV SendChatVoice:[[DataControl shareControl].chatDS SendChatContent]                                                withMail:[DataControl shareControl].currentFI.mail];
            }
        }
            break;
        case LC_NEARPEOPLE:
        {
            [[netManager sharedNetManager].sendNP SendLocation:[DataControl shareControl].mlatitude
                                          withLongitude:[DataControl shareControl].mlongitude
                                       withNPFilterType:getNearPeopleFilterType()];
        }
            break;
        case LC_SENDREQUESTINFO:
        {
            
            for (int imageCnt = 0; imageCnt< [[DataControl shareControl].firendInfoSendArr count]; imageCnt++) {
                NSString *mail = [[DataControl shareControl].firendInfoSendArr objectAtIndex:imageCnt];
                [[netManager sharedNetManager].sendFirendS SendSearchFirendP:mail];
            }
            
            [[DataControl shareControl].firendInfoSendArr removeAllObjects];
        }
            break;
        case LC_SENDREQUESTHI:
        {
            for (int imageCnt = 0; imageCnt< [[DataControl shareControl].headImageSendArr count]; imageCnt++) {
                NSString *mail = [[DataControl shareControl].headImageSendArr objectAtIndex:imageCnt];
                [[netManager sharedNetManager].sendHISmall SendRequestHeadImageP:mail];
            }
            
            [[DataControl shareControl].headImageSendArr removeAllObjects];
        }
            break;
        case LC_DELETECACHE:
        {
            
            NSArray *removeArr = [[DataControl shareControl].HeadImageDic allKeys];
            for (NSString *mail in removeArr)
            {
                if (![mail isEqualToString:[DataControl shareControl].personDS.uinfo.mail])
                {
                    [[FileManager shareControl] deleteFile:
                     [[DataControl shareControl].HeadImageDic objectForKey:mail]];
                    
                    [[DataBaseCtrol shareControl].headimagedatabase deleteHImageByMail:mail];
                    
                    [[DataControl shareControl].HeadImageDic removeObjectForKey:mail];
                }
            }
            
            removeArr = [[DataControl shareControl].bigHIDS.bigimageDic allKeys];
            for (NSString *mail in removeArr)
            {
                if (![mail isEqualToString:[DataControl shareControl].personDS.uinfo.mail])
                {
                    BigImageInfo *delinfo = [[DataControl shareControl].bigHIDS.bigimageDic objectForKey:mail];
                    [[FileManager shareControl] deleteFile:delinfo.url];
                    
                    [[DataBaseCtrol shareControl].bigimagedatabase deleteHImageByMail:mail];
                    
                    [[DataControl shareControl].bigHIDS.bigimageDic removeObjectForKey:mail];
                }
            }
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_DELETECACHEOVER];
        }
            break;
        case LC_REPORT:
        {
            if ([DataControl shareControl].reportMail != nil)
            {
                [[netManager sharedNetManager].sendR SendReportP:
                 [DataControl shareControl].reportMail];
            }
        }
            break;
        case LC_SETMAIL:
        {
            [[netManager sharedNetManager].sendPersonSM SendSetMailP:[DataControl shareControl].personDS.uinfo.mymail];
        }
            break;
        case LC_NET_RECONNECT:
        {
            if ([DataControl shareControl].iGameState != GMS_CHANGEGROUND) {
                [[netManager sharedNetManager] ReConnect];
            }
        }
            break;
        case LC_LOGINAUTO:
        {
            if ([DataControl shareControl].personDS.uinfo.ppNumber != -1) {
                [[DataControl shareControl] SetLoginType:LT_PP];
                
                [[netManager sharedNetManager].sendloginpp SendLoginWithPPP:
                 [DataControl shareControl].personDS.uinfo.ppNumber
                                                                     withPS:
                 [DataControl shareControl].personDS.uinfo.ps];
            }
            else if(![[DataControl shareControl].personDS.uinfo.mail isEqualToString:@""]){
                [[DataControl shareControl] SetLoginType:LT_MAIL];
                
                [[netManager sharedNetManager].sendlonginbymail sendLoginByMail:
                 [DataControl shareControl].personDS.uinfo.mail
                                                                         withPS:
                 [DataControl shareControl].personDS.uinfo.ps];
            }
        }
            break;
        case LC_FCR_DOWNLOAD:
        {
            [[DataControl shareControl].fcLocalRecDS DownLoadResource];
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_REFRESHCURVIEW];
        }
            break;
        case LC_FCR_GETGBI:
        {
            NSMutableArray *arr = [[DataControl shareControl].fcBgImageDS getSearchArr];
            
            if (arr != nil) {
                for (NSString *mail in arr) {
                    [[netManager sharedNetManager].sendFCGetBGI SendFCGetBgImage:
                     mail];
                }
            }
            
        }
            break;
        case LC_FCR_SENDBGI:
        {
            [[netManager sharedNetManager].sendFCBgI SendFCBgImage:
             [[DataControl shareControl].fircirDS getBGImage]];
        }
            break;
        case LC_READVER:
        {
            [[netManager sharedNetManager].sendp SendMainIndex:ID_MI_VERSION
                                               WithSecondIndex:VT_IOS];
        }
            break;
        case LC_GIFT_DOWNIMAGE:
        {
            [[DataControl shareControl].giftlistDS DownLoadImageArr];
        }
            break;
        case LC_GIFT_DOWNGIFTIMAGE:
        {
            [[DataControl shareControl].giftInfoDS DownLoadImage];
        }
            break;
        case LC_FIR_DELNEW:
        {
            NSString *newsid = [DataControl shareControl].fircirDS.strdelid;
            
            if (newsid != nil && ![newsid isEqualToString:@""]) {
                [[DataBaseCtrol shareControl].fircirdatabase DelNewsByID:newsid];
                [[DataBaseCtrol shareControl].fircirlocaldatabase deleteByID:newsid];
                [[DataBaseCtrol shareControl].fircircommentdatabase delCommentByNewsID:newsid];
                [[DataBaseCtrol shareControl].fircirPIDdatabase delByNewsID:newsid];
                [[DataBaseCtrol shareControl].fircirnewdatabase delNewsByID:newsid];
                [[DataBaseCtrol shareControl].fircirnewcommentdatabase delNewCommentByNewId:newsid];
                [[DataBaseCtrol shareControl].fircirmailnewsdatabase delMailByNewId:newsid];
                
                [[DataControl shareControl].fircirDS DelFirCirArrByID:newsid];
                [[DataControl shareControl].fcLocalRecDS DelLocalByID:newsid];
                [[DataControl shareControl].fcnewsDS DelNewsByID:newsid];
                [[DataControl shareControl].fcnewsmailDS DelByNewsID:newsid];
                
                [[DataControl shareControl].fcPersonDS DelCurshowArrObject:newsid];
                [[DataControl shareControl].fcPersonDS AddDelIDArr:newsid];
                
                [[DataControl shareControl].UICommendDS addUICommand:UC_REFRESHCURVIEW];
            }
            
            
        }
            break;
        case LC_DB_GIFT_INIMAGEPATH:
        {
            [[DataBaseCtrol shareControl].giftinfoImageDB insertPath:
             [DataControl shareControl].giftInfoDS.strDBUrl
                                                              withID:
             [DataControl shareControl].giftInfoDS.strDBID];
        }
            break;
        case LC_DB_GIFT_INMUSICPATH:
        {
            [[DataBaseCtrol shareControl].giftinfoMusicDB insertPath:
             [DataControl shareControl].giftInfoDS.strDBMusicUrl
                                                              withID:
             [DataControl shareControl].giftInfoDS.strDBMusicID];
        }
            break;
            case LC_CHAT_PROCESSMSG:
        {
            if ([DataControl shareControl].chatDS.currentChatArr != nil) {
                for (ChatContentInfo *ccinfo in [DataControl shareControl].chatDS.currentChatArr) {
                    if (ccinfo != nil) {
                        if(ccinfo.ichatReadState == CRS_UNREAD)
                        {
                            if (ccinfo.chatType != CT_VOICE) {
                                ccinfo.ichatReadState = CRS_READ;
                                [[DataBaseCtrol shareControl].chatuserdatabase UpdateChatState:ccinfo];
                                
                                [[netManager sharedNetManager].sendCR sendChatAlreadyRead:ccinfo.strid];
                            }
                        }
                    }
                }
            }
        }
            break;
        case LC_CHAT_MODIFYSTATE:
        {
            [[DataControl shareControl].chatDS ProcessModifyState];
        }
            break;
        case LC_BIGIMAGE_SEARCH:
        {
            [[DataControl shareControl].bigHIDS SendBigImageRequest];
        }
            break;
        default:
            break;
    }
}

@end
